/* Copyright (C) 2008, 2009, 2010 Faindu GmbH, Switzerland.
* All Rights Reserved. The following is Source Code and is subject to all restrictions 
* on such code as contained in the license accompanying this project. */
package blocstac.editimage.base
{

import blocstac.api.blocbox.ImageLoader;

import flash.display.Bitmap;
import flash.events.TimerEvent;
import flash.utils.Timer;

import flexunit.framework.TestCase;

import mx.core.BitmapAsset;

public class ImageLoaderImplTest extends TestCase
{
	
	[Embed(source="zug.jpg")]
	private var zugRaw:Class;    
	
    //==========================================================================
    //
    //  Test set up and tear down
    //
    //--------------------------------------------------------------------------  
	private var imageLoader:ImageLoaderImpl
	private const timeOutMillis:Number = 300
	private var timer:Timer
	private const ANY_URL:String = "http://hugo.blocstac.com/hugo/b/whatever"
	private var zugBitmap:Bitmap
	
	override public function setUp(): void
	{
		var zugAsset:BitmapAsset = new zugRaw() as BitmapAsset
		zugBitmap = new Bitmap(zugAsset.bitmapData)

		imageLoader = new ImageLoaderImpl(null)
	}
	
	override public function tearDown():void
	{
	}
	
    //==========================================================================
    //
    //  Tests
    //
    //--------------------------------------------------------------------------  
	[Test]
	public function test_fromUrl(): void
	{
		imageLoader.fromUrl("zug.jpg")
		
		timer = new Timer(timeOutMillis, 1)
		timer.addEventListener(TimerEvent.TIMER_COMPLETE, addAsync(test_fromUrlHandler, timeOutMillis+100))
		timer.start()
	}
	private function test_fromUrlHandler(event:TimerEvent):void {
		assertNotNull(imageLoader.image)
	}
	
	[Test]
	public function test_fromBitmap():void {
		imageLoader.fromBitmap(zugBitmap, ANY_URL)
		
		assertEquals(ANY_URL, imageLoader.url)
		assertTrue(zugBitmap === imageLoader.image)
	}
	
	[Test]
	public function test_whenCompleted():void {
		var completedHasBeenCalled:int = 0
		var callMe:Function = function ():void {
			completedHasBeenCalled++
		}
		imageLoader.whenCompleted(callMe)
		imageLoader.fromBitmap(zugBitmap, ANY_URL)
		
		assertTrue(completedHasBeenCalled == 1)
	}
	
	[Test]
	public function test_whenCompleted_with_2_functions():void {
		var completedHasBeenCalled:int = 0
		var callMe:Function = function ():void {
			completedHasBeenCalled++
		}
		imageLoader.whenCompleted(callMe)
		imageLoader.whenCompleted(callMe)
		imageLoader.fromBitmap(zugBitmap, ANY_URL)
		
		assertTrue(completedHasBeenCalled == 2)
	}
	
	[Test]
	public function test_clone():void
	{
		imageLoader.fromBitmap(zugBitmap, ANY_URL)
		var clone:ImageLoader = imageLoader.clone()
		
		assertEquals(imageLoader.url, clone.url)
		assertTrue(imageLoader.image !== clone.image)
	}

}
}